package com.es.spring_es.rabbitmq.services;

import com.es.spring_es.rabbitmq.msg.Area;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


@Component
public class MQSendService implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback {

    public RabbitTemplate shanghairabbitTemplate;
    public RabbitTemplate beijingrabbitTemplate;
    private Gson gson=new Gson();

    private static final Logger logger = LoggerFactory.getLogger(MQSendService.class);

    @Resource
    public void shangHaiRabbitTemplate(@Qualifier("shanghairabbitTemplate") RabbitTemplate shanghairabbitTemplate) {
        this.shanghairabbitTemplate = shanghairabbitTemplate;
        shanghairabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack) {
                logger.error("**error**_sh");
            }
        });
        shanghairabbitTemplate.setReturnCallback((message,  replyCode,  replyText,  exchange,  routingKey) ->{
            logger.error("***error***_sh");
        });

    }

  /*  @Resource
    public void beiJingRabbitTemplate(@Qualifier("beijingrabbitTemplate") RabbitTemplate beijingrabbitTemplate) {
        this.beijingrabbitTemplate = beijingrabbitTemplate;
        beijingrabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack) {
                logger.error("**error**_bj");
            }
        });
        beijingrabbitTemplate.setReturnCallback((message,  replyCode,  replyText,  exchange,  routingKey) ->{
            logger.error("***error***_bj");
        });

    }*/



    public void sendJsonToExchange(Area area,String exchangeName, String routingKey, Object data){
        RabbitTemplate rabbitTemplate = getArea(area);
        sendToExchange(rabbitTemplate,exchangeName,routingKey,data, MessageProperties.CONTENT_TYPE_JSON,null,null);
    }

    public void sendJsonToExchange(Area area, String exchangeName, String routingKey, Object data, String expiration){
        RabbitTemplate rabbitTemplate = getArea(area);
        sendToExchange(rabbitTemplate,exchangeName,routingKey,data, MessageProperties.CONTENT_TYPE_JSON,expiration,null);
    }

    public void sendJsonToExchange(Area area,String exchangeName, String routingKey, Object data,String expiration,HashMap<String,String> heards){
        RabbitTemplate rabbitTemplate = getArea(area);
        sendToExchange(rabbitTemplate,exchangeName,routingKey,data, MessageProperties.CONTENT_TYPE_JSON,expiration,heards);
    }

    public void sendToExchange(RabbitTemplate rabbitTemplate, String exchangeName, String routingKey, Object data, String contentType, String expiration, HashMap<String,String> heards){
        try {
            rabbitTemplate.convertAndSend(exchangeName,routingKey,data,message-> {
                MessageProperties messageProperties= message.getMessageProperties();
                messageProperties.setContentType(contentType);
                messageProperties.setContentEncoding("UTF-8");
                messageProperties.setTimestamp(new Date());
                if (expiration!=null){
                    messageProperties.setExpiration(expiration);
                }
                if (heards!=null){
                    Iterator<Map.Entry<String, String>> it = heards.entrySet().iterator();
                    while (it.hasNext()){
                        Map.Entry<String, String> next = it.next();
                        String key = next.getKey();
                        String value = next.getValue();
                        messageProperties.setHeader(key,value);
                    }
                }
                return message;
            });
            logger.info("send success, data: "+gson.toJson(data)+" exchangeName: "+exchangeName+"  routingKey: "+routingKey);
        }catch (Exception e){
            logger.error("send error, data: "+gson.toJson(data)+" exchangeName: "+ exchangeName+"  routingKey: "+routingKey+"  "+e.getMessage());
        }

    }


    public RabbitTemplate getArea(Area area){
          switch (area){
              case SHANGHAI:
                  logger.info("to shanghai");
                  return shanghairabbitTemplate;
              /*case BEIJING:
                  logger.info("to beijing");
                  return beijingrabbitTemplate;*/
              default:
                  return null;
          }
    }


    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("adsffffffffffsadfasdf"+ack);

    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("sfasfasdfsdfsdfsdf");
    }
}
